package com.zqxq.auth.web;

import com.zqxq.auth.cache.UserFuncCache;
import com.zqxq.auth.entity.po.Role;
import com.zqxq.auth.entity.module.Code;
import com.zqxq.auth.entity.module.Result;
import com.zqxq.auth.service.IFuncService;
import com.zqxq.auth.service.IRoleService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2018/01/18 17:07
 */
@RestController
@RequestMapping(value = "/role", produces = "application/json")
public class RoleController {
    private static Logger logger = LoggerFactory.getLogger(RoleController.class);
    @Autowired
    private IRoleService roleService;
    @Autowired
    private UserFuncCache userFuncCache;
    @Autowired
    private IFuncService funcService;
    /**
     * 查询角色列表
     * @return
     */
    @PostMapping("/getRoleList")
    @ApiOperation(value="查询角色列表", notes="查询角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "当前页数，以1开始", required = true, dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "PageSize", value = "页面显示长度", required = true, dataType = "Integer", paramType = "query")
    })
    public Result getRoleList(Integer pageNo, Integer pageSize, HttpServletRequest request){
        return roleService.getRoleList(pageNo, pageSize, userFuncCache.getUserAdmin(request).getRole_id());
    }

    /**
     * 修改角色状态
     * @param role_id
     * @param role_status
     * @return
     */
    @PostMapping("/updateRoleStatus")
    @ApiOperation(value="修改角色状态", notes="修改角色状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role_id", value = "角色id", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "role_status", value = "状态：0=未启用，1=启用", required = true, dataType = "Integer", paramType = "query")
    })
    public Result updateRoleStatus(Long role_id, Integer role_status){
        if (role_id == null) {
            return new Result(Code.FAIL, "角色id不能为空");
        }
        if (role_status == null || role_status > 1 || role_status < 0) {
            return new Result(Code.FAIL, "角色状态非法");
        }
        return roleService.updateRoleStatus(role_id, role_status);
    }

    @ApiOperation(value = "保存角色",notes = "保存角色")
    @PostMapping("/saveRole")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role_id", value = "角色id", dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "role_name", value = "角色名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "role_status", value = "状态：0=未启用，1=启用", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "order_no", value = "序号", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "funcIds", value = "功能ids,以逗号分割", dataType = "String", paramType = "query")
    })
    public Result saveRole(Role role, String funcIds, HttpServletRequest request) {
        logger.info("saveRole入参：role={},funcIds={}", role.toString(), funcIds);
        return roleService.saveRole(role, funcIds, userFuncCache.getUserAdmin(request));
    }

    @PostMapping("/getUserRoleFunc")
    @ApiOperation(value="获取用户角色功能", notes="获取用户角色功能")
    public Result getUserRoleFunc(HttpServletRequest request) {
        Long role_id = userFuncCache.getUserAdmin(request).getRole_id();
        return new Result(funcService.getUserRoleFunc(role_id));
    }

    @ApiOperation(value = "验证角色名称是否通过",notes = "验证角色名称是否通过，data=true 通过")
    @PostMapping("/isRoleNamePass")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role_name", value = "角色名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "role_id", value = "角色id", dataType = "Long", paramType = "query")

    })
    public Result isRoleNamePass(String role_name, Long role_id) {
        return roleService.isRoleNamePass(role_name, role_id);
    }

    @ApiOperation(value = "获取用户启用中的角色列表",notes = "获取用户启用中的角色列表")
    @PostMapping("/getUsingRoleList")
    public Result getUsingRoleList(HttpServletRequest request) {
        return roleService.getUsingRoleList(userFuncCache.getUserAdmin(request));
    }

    @ApiOperation(value = "根据权限id，查询对应功能",notes = "根据权限id，查询对应功能")
    @PostMapping("/getRoleFuncByRoleId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role_id", value = "角色id", dataType = "Long", paramType = "query")
    })
    public Result getRoleFuncByRoleId(Long role_id) {
       return roleService.getRoleFuncByRoleId(role_id);
    }

}
